#include "action.h"

const QString Action::DEFUAL_ACTION_NAME = "NewAction";

Action::Action()
    : m_name(DEFUAL_ACTION_NAME),
      m_frameNum(0),
      m_delay(0.07f),
      m_currentFrame(NULL)
{
}

Action::~Action()
{
    PlistFrameInfoList::iterator it;
    for(it = m_frameList.begin(); it != m_frameList.end(); it++)
    {
        CC_SAFE_DELETE_NULL(*it);
    }
    m_frameList.clear();
    m_currentFrame = NULL;
}

bool Action::isValidIndex(int index)
{
   if(index < 0 || index >= m_frameList.count() || m_frameList.count() == 0) return false;

   return true;
}


void Action::appendFrameList(const PlistFrameInfoList& frameNameList)
{
    for(int index = 0; index < frameNameList.count(); index ++)
    {
        PlistFrameInfo* newInfo = new PlistFrameInfo();
        PlistFrameInfo* oldInfo = frameNameList.at(index);
        newInfo->name = oldInfo->name;
        newInfo->plistName = oldInfo->plistName;
        newInfo->plistPngName = oldInfo->plistPngName;
        newInfo->sizeRect = oldInfo->sizeRect;
        newInfo->isRotated = oldInfo->isRotated;
        newInfo->sizeRect = oldInfo->sizeRect;
        newInfo->offset = oldInfo->offset;
        newInfo->sourceColorRect = oldInfo->sourceColorRect;
        newInfo->sourceRectPoint = oldInfo->sourceRectPoint;

        m_frameList.append(newInfo);        
        if(index == 0)
        {
            m_currentFrame = newInfo;
        }
    }
}

void Action::removeFrameByIndex(int index)
{
    if(!isValidIndex(index)) return;
    PlistFrameInfo* frameInfo = m_frameList.at(index);
    CC_SAFE_DELETE_NULL(frameInfo);

    m_frameList.removeAt(index);

    if(m_frameList.count() == 0)
    {
        m_currentFrame = NULL;
    }
}

bool Action::setCurrentFrameByIndex(int index)
{
    if(!isValidIndex(index)) return NULL;

    m_currentFrame = m_frameList.at(index);
}

QString Action::getFrameNameByIndex(int index)
{
    if(!isValidIndex(index)) return NULL;

    return m_frameList.at(index)->name;
}

void Action::convertRectToString(QRectF &rect, QDomElement &element, QString attributeName)
{
    QString size;
    size.append(QString::number(rect.x())).append(",");
    size.append(QString::number(rect.y())).append(",");
    size.append(QString::number(rect.width())).append(",");
    size.append(QString::number(rect.height()));
    element.setAttribute(attributeName, size);
}

void Action::convertPointToString(QPointF &point, QDomElement &element, QString attributeName)
{
    QString pointSize;
    pointSize.append(QString::number(point.x())).append(",");
    pointSize.append(QString::number(point.y()));

    element.setAttribute(attributeName,pointSize);
}



void Action::convertStringToRect(QRectF &rect, QDomElement &element, QString attributeName)
{
     QStringList sizeRect = element.attribute(attributeName).split(",");
     rect.setX(sizeRect.at(XSLOT).toInt());
     rect.setY(sizeRect.at(YSLOT).toInt());
     rect.setWidth(sizeRect.at(WSLOT).toInt());
     rect.setHeight(sizeRect.at(HSLOT).toInt());
}

void Action::convertStringToPoint(QPointF &point, QDomElement &element, QString attributeName)
{
    QStringList pointSize = element.attribute(attributeName).split(",");
    point.setX(pointSize.at(XSLOT).toInt());
    point.setY(pointSize.at(YSLOT).toInt());
}

void Action::convertFramenToDom(QDomElement &parent, QDomDocument* doc)
{
    for(int index = 0 ; index < m_frameList.count(); index ++)
    {
        QDomElement frameElement = doc->createElement("frame");


        convertRectToString(m_frameList.at(index)->sizeRect, frameElement,QString("size"));
        convertPointToString(m_frameList.at(index)->offset, frameElement,QString("offset"));
        convertRectToString(m_frameList.at(index)->sourceColorRect, frameElement,QString("sourceColorRect"));
        convertPointToString(m_frameList.at(index)->sourceRectPoint, frameElement,QString("sourceSize"));

        int isRotated = m_frameList.at(index)->isRotated ? 1 : 0;
        frameElement.setAttribute("isRotated", isRotated);

        frameElement.setAttribute("plistName",m_frameList.at(index)->plistName);
        frameElement.setAttribute("plistPngName",m_frameList.at(index)->plistPngName);

        QDomText textNode = doc->createTextNode(m_frameList.at(index)->name);
        frameElement.appendChild(textNode);

        parent.appendChild(frameElement);
    }
}

void Action::loadFromDom(const QDomElement &parentElement)
{
    QDomNodeList frameList = parentElement.childNodes();
    for(int index = 0; index < frameList.count(); index++)
    {
        PlistFrameInfo* info = new PlistFrameInfo();
        QDomElement infoElement = frameList.at(index).toElement();

        info->name = infoElement.text();
        info->plistName = infoElement.attribute("plistName");
        info->plistPngName = infoElement.attribute("plistPngName");

        convertStringToRect(info->sizeRect,infoElement,QString("size"));
        convertStringToPoint(info->offset,infoElement,QString("offset"));
        convertStringToRect(info->sourceColorRect,infoElement,QString("sourceColorRect"));
        convertStringToPoint(info->sourceRectPoint,infoElement,QString("sourceSize"));

        info->isRotated = infoElement.attribute("isRotated").toInt() == 1;

        m_frameList.append(info);
    }
}

void Action::appendPlistInfo(PlistFrameInfoList infoList)
{
    for(int index = 0; index < infoList.count(); index ++)
    {
        PlistFrameInfo* info = infoList.at(index);
        if(m_plistFileName.indexOf(info->plistName) == -1)
        {
            m_plistFileName.append(info->plistName);
        }

        if(m_plistPngName.indexOf(info->plistPngName) == -1)
        {
            m_plistPngName.append(info->plistPngName);
        }
    }
}

